package com.starrin.trashtalk.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by larsstarrin on 31/08/14.
 */

@Entity
@Table(name = "SingleMatch")
public class SingleMatch {
    private Long id;
    private Player player1;
    private Player player2;
    private Player winner;
    private Date date;
    private List<SingleSet> singleSets;

    /*
    private List<MatchComment> comments;
    private String venue;
    private String comment;
    */

    @Id
    @GeneratedValue
    @Column(name = "single_match_id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne
    @JoinColumn(name = "player1_id")
    public Player getPlayer1() {
        return player1;
    }

    public void setPlayer1(Player player1) {
        this.player1 = player1;
    }

    @OneToOne
    @JoinColumn(name = "player2_id")
    public Player getPlayer2() {
        return player2;
    }

    public void setPlayer2(Player player2) {
        this.player2 = player2;
    }

    @OneToOne
    @JoinColumn(name = "winner_id")
    public Player getWinner() {
        return winner;
    }

    public void setWinner(Player winner) {
        this.winner = winner;
    }

    @Column(name = "match_date")
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "singleMatch", cascade = CascadeType.ALL)
    public List<SingleSet> getSingleSets() {
        return singleSets;
    }

    public void setSingleSets(List<SingleSet> singleSets) {
        this.singleSets = singleSets;
    }

    /*
    public List<MatchComment> getComments() {
        return comments;
    }

    public void setComments(List<MatchComment> comments) {
        this.comments = comments;
    }

    public String getVenue() {
        return venue;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    */

    public void addSet(SingleSet set) {
        if (singleSets == null) {
            singleSets = new ArrayList<>();
        }
        set.setSingleMatch(this);
        singleSets.add(set);
    }

    public String setsAsString() {
        String string = "";
        for (SingleSet set : getSingleSets()) {
            if (!string.equals("")) {
                string += ", ";
            }
            string += set.getGamesPlayer1() + "-" + set.getGamesPlayer2();
        }
        return string;
    }

    public boolean homeVictory() {
        return winner != null && (winner.equals(player1));
    }
    public boolean awayVictory() {
        return winner != null && (winner.equals(player2));
    }
}
